\section{Copy and move}

The standard offers a wide range of copy algorithms in roughly three categories: simple copies and moves, selective copies and copies with reordering.

\subsection{\texorpdfstring{\cpp{std::copy}, \cpp{std::move}}{\texttt{std::copy}, \texttt{std::move}}}
\index{\cpp{std::copy}}
\index{\cpp{std::move}}

The \cpp{std::copy} and \cpp{std::move} algorithms provide a forward copy and move. The direction is important for overlapping ranges, so we do not overwrite the yet-to-be copied elements.

For the forward direction, the output iterator is not permitted to be within \texttt{[first, last)} (of the input range). Consequently, only the tail of the output range can overlap with the input range.

\cppversions{\texttt{copy}}{\CC98}{\CC20}{\CC17}{\CC20}
\cppversions{\texttt{move}}{\CC11}{\CC20}{\CC17}{\CC20}

\constraints{\texttt{input\_range -> output\_iterator}}{\texttt{forward\_range -> forward\_iterator}}{}{}

\begin{codebox}[]{\href{https://compiler-explorer.com/z/d5c855Gcv}{\ExternalLink}}
\footnotesize Example of a non-overlapping and permitted overlapping case of \cpp{std::copy}.
\tcblower
\cppfile{code_examples/algorithms/copy_code.h}
\end{codebox}

Move operates identically, except it casts each element to an rvalue before the assignment, turning copies into moves.

\begin{codebox}[]{\href{https://compiler-explorer.com/z/cP1P3e6q3}{\ExternalLink}}
\footnotesize Example of using \cpp{std::move}.
\tcblower
\cppfile{code_examples/algorithms/move_code.h}
\end{codebox}

Significantly, whether std::move will move depends on the underlying element type. If the underlying type is copy-only, std::move will behave identically to std::copy.

\begin{codebox}[]{\href{https://compiler-explorer.com/z/9ejzxEr8K}{\ExternalLink}}
\footnotesize Example of using \cpp{std::move} with a copy-only type.
\tcblower
\cppfile{code_examples/algorithms/move_nomove_code.h}
\end{codebox}

\subsection{\texorpdfstring{\cpp{std::copy_backward}, \cpp{std::move_backward}}{\texttt{std::copy\_backward}, \texttt{std::move\_backward}}}
\index{\cpp{std::copy_backward}}
\index{\cpp{std::move_backward}}

The \cpp{std::copy_backward} and \cpp{std::move_backward} are variants that copy in the opposite direction, starting at the back of the range. Because of this, the head of the output range can now overlap with the input range.

\cppversions{\texttt{copy\_backward}}{\CC98}{\CC20}{N/A}{\CC20}
\cppversions{\texttt{move\_backward}}{\CC11}{\CC20}{N/A}{\CC20}

\constraints{\texttt{bidirectional\_range -> bidirectional\_iterator}}{}{}{}

The output iterator cannot be within \cpp{(first, last]} and will be treated as the end iterator for the destination range, meaning that the algorithm will write the first value to \cpp{std::prev(end)}.

\begin{codebox}[]{\href{https://compiler-explorer.com/z/TThv49Gqa}{\ExternalLink}}
\footnotesize Example of a non-overlapping and permitted overlapping case of \cpp{std::copy_backward}.
\tcblower
\cppfile{code_examples/algorithms/copy_backward_code.h}
\end{codebox}

\subsection{\texorpdfstring{\cpp{std::copy_n}}{\texttt{std::copy\_n}}}
\index{\cpp{std::copy_n}}

The \cpp{std::copy_n} algorithm is the counted variant of \cpp{std::copy} that accepts an input range specified using an iterator and the number of elements.

\cppversions{\texttt{copy\_n}}{\CC11}{\CC20}{\CC17}{\CC20}
\constraints{\texttt{input\_iterator -> output\_iterator}}{\texttt{forward\_iterator -> forward\_iterator}}{}{}

The algorithm cannot check whether the requested count is valid and does not go out of bounds, so this burden is on the caller.

\begin{codebox}[]{\href{https://compiler-explorer.com/z/KGYab3WEr}{\ExternalLink}}
\footnotesize Example of using \cpp{std::copy_n}.
\tcblower
\cppfile{code_examples/algorithms/copy_n_code.h}
\end{codebox}

\subsection{\texorpdfstring{\cpp{std::copy_if}, \cpp{std::remove_copy}, \cpp{std::remove_copy_if}}{\texttt{std::copy\_if}, \texttt{std::remove\_copy}, \texttt{std::remove\_copy\_if}}}
\index{\cpp{std::copy_if}}
\index{\cpp{std::remove_copy}}
\index{\cpp{std::remove_copy_if}}

The \cpp{std::copy_if}, \cpp{std::remove_copy} and \cpp{std::remove_copy_if} are selective copy algorithms.

\cppversions{\texttt{copy\_if}}{\CC11}{\CC20}{\CC17}{\CC20}
\cppversions{\texttt{remove\_copy}}{\CC98}{\CC20}{\CC17}{\CC20}
\cppversions{\texttt{remove\_copy\_if}}{\CC98}{\CC20}{\CC17}{\CC20}

\constraints{\texttt{input\_range -> output\_range}}{\texttt{forward\_range -> forward\_iterator}}{N/A}{\texttt{unary\_predicate}}

The \cpp{std::remove_copy} algorithm will copy elements that do not match the provided value. The \cpp{std::copy_if} and \cpp{std::remove_copy_if} algorithms will copy elements based on a predicate, with \cpp{std::copy_if} copying elements for which the predicate returns true and \cpp{std::remove_copy_if} copying elements for which the predicate returns false.

\begin{codebox}[breakable]{\href{https://compiler-explorer.com/z/vY6bTcqsr}{\ExternalLink}}
\footnotesize Example demonstrating differences between \cpp{std::copy_if}, \cpp{std::remove_copy} and \cpp{std::remove_copy_if}.
\tcblower
\cppfile{code_examples/algorithms/copy_if_code.h}
\end{codebox}

\subsection{\texorpdfstring{\cpp{std::sample}}{\texttt{std::sample}}}
\index{\cpp{std::sample}}

The \cpp{std::sample} algorithm is a random selective copy algorithm. The algorithm will copy a random selection of N elements from the source range to the destination range utilising the provided random number generator.

\cppversions{\texttt{sample}}{\CC17}{N/A}{N/A}{\CC20}
\constraints{\texttt{forward\_range -> output\_iterator}\newline \texttt{input\_range -> random\_access\_iterator}}{}{}{}

The two domains of this algorithm are due to the stable nature of the sampling, maintaining the order of elements from the source range. This feature requires either the input range to be at least a forward range or the destination range needs to be a random-access range.

\begin{codebox}[]{\href{https://compiler-explorer.com/z/75WPEfjM9}{\ExternalLink}}
\footnotesize Example of using \cpp{std::sample}.
\tcblower
\cppfile{code_examples/algorithms/sample_code.h}
\end{codebox}

\subsection{\texorpdfstring{\cpp{std::replace_copy}, \cpp{std::replace_copy_if}}{\texttt{std::replace\_copy}, \texttt{std::replace\_copy\_if}}}
\index{\cpp{std::replace_copy}}
\index{\cpp{std::replace_copy_if}}

The \cpp{std::replace_copy} and \cpp{std::replace_copy_if} algorithms operate like \cpp{std::copy}; however, they will copy a provided value instead of specific elements.

For \cpp{std::replace_copy}, the algorithm will replace elements matching a value for \cpp{std::replace_copy_if} the algorithm will replace elements for which the predicate evaluates to true.

\cppversions{\texttt{replace\_copy}}{\CC98}{\CC20}{\CC17}{\CC20}
\cppversions{\texttt{replace\_copy\_if}}{\CC98}{\CC20}{\CC17}{\CC20}
\constraints{\texttt{input\_range -> output\_iterator}}{\texttt{forward\_range -> forward\_iterator}}{N/A}{\texttt{unary\_predicate}}

\begin{codebox}[]{\href{https://compiler-explorer.com/z/TPTq4q8rr}{\ExternalLink}}
\footnotesize Example of using \cpp{std::replace_copy} and \cpp{std::replace_copy_if}.
\tcblower
\cppfile{code_examples/algorithms/replace_copy_code.h}
\end{codebox}

\subsection{\texorpdfstring{\cpp{std::reverse_copy}}{\texttt{std::reverse\_copy}}}
\index{\cpp{std::reverse_copy}}

The \cpp{std::reverse_copy} algorithm copies elements in reverse order.

\cppversions{\texttt{reverse\_copy}}{\CC98}{\CC20}{\CC17}{\CC20}
\constraints{\texttt{bidirectional\_range -> output\_iterator}}{\texttt{bidirectional\_range -> forward\_iterator}}{}{}

Not to be confused with the \cpp{std::copy_backwards}, which copies elements in the original order. The \cpp{std::reverse_copy} does not permit the source, and the destination ranges to overlap.

\begin{codebox}[]{\href{https://compiler-explorer.com/z/1YM9WevcT}{\ExternalLink}}
\footnotesize Example of using \cpp{std::reverse_copy}.
\tcblower
\cppfile{code_examples/algorithms/reverse_copy_code.h}
\end{codebox}

\subsection{\texorpdfstring{\cpp{std::rotate_copy}}{\texttt{std::rotate\_copy}}}
\index{\cpp{std::rotate_copy}}

The \cpp{std::rotate_copy} algorithm will copy elements \cpp{[middle, last)}, followed by \cpp{[first, middle)}, which mirrors the behaviour of the \cpp{std::rotate} algorithm.

\cppversions{\texttt{rotate\_copy}}{\CC98}{\CC20}{\CC17}{\CC20}
\constraints{\texttt{(forward\_range, forward\_iterator) -> output\_iterator}}{\texttt{(forward\_range, forward\_iterator) -> forward\_iterator}}{}{}

The input and output ranges cannot overlap.

\begin{codebox}[]{\href{https://compiler-explorer.com/z/MWf5q3r9d}{\ExternalLink}}
\footnotesize Example of using \cpp{std::rotate_copy}.
\tcblower
\cppfile{code_examples/algorithms/rotate_copy_code.h}
\end{codebox}